# -*- coding: utf-8 -*-
"""
Created on Wed Aug 22 09:10:30 2018

@author: Who

币圈财经
http://www.biquancj.com/

"""

from WriteData import writedata
from Tk import genearteMD5
from GetText import get_html_text
from bs4 import BeautifulSoup
from cs import day


def get_dates_zx(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    text = get_html_text(url, headers)
    if not text:
        return None
    soup = BeautifulSoup(text, 'lxml')
    data = soup.select('.news-timer')
    dates = [date.get_text()[-19:-9] for date in data]
    return dates


def get_dates_kx():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    url = 'http://www.biquancj.com/lives/lives.php'
    text = get_html_text(url, headers)
    if not text:
        return []
    soup = BeautifulSoup(text, 'lxml')
    data = soup.select('.time')
    dates = [date.get_text() for date in data]
    return dates


def count_process_one():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:
        url = 'http://www.biquancj.com/news/index_{}.html'
        url = url.format(a)
        if url == 'http://www.biquancj.com/news/index_1.html':
            url = 'http://www.biquancj.com/news/index.html'
        dates = get_dates_zx(url)
        if not dates:
            break
        print('---- 币圈财经资讯获取第{}页----'.format(a))
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '币圈财经'
    t1 = count_process_one()
    t2 = len(get_dates_kx())
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2, "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
